package cn.stylefeng.guns.modular.rms.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.rms.entity.Company;
import cn.stylefeng.guns.modular.rms.mapper.CompanyMapper;
import cn.stylefeng.guns.modular.rms.model.params.CompanyParam;
import cn.stylefeng.guns.modular.rms.model.params.CompanyPositionParam;
import cn.stylefeng.guns.modular.rms.model.result.CompanyPositionResult;
import cn.stylefeng.guns.modular.rms.model.result.CompanyResult;
import cn.stylefeng.guns.modular.rms.service.CompanyPositionService;
import  cn.stylefeng.guns.modular.rms.service.CompanyService;
import cn.stylefeng.guns.modular.ums.model.params.MemberParam;
import cn.stylefeng.guns.modular.ums.service.MemberService;
import cn.stylefeng.guns.modular.wms.model.params.EduExpParam;
import cn.stylefeng.guns.modular.wms.model.params.WorkExpParam;
import cn.stylefeng.guns.modular.wms.model.result.EduExpResult;
import cn.stylefeng.guns.modular.wms.model.result.ResumeResult;
import cn.stylefeng.guns.modular.wms.model.result.WorkExpResult;
import cn.stylefeng.guns.utils.MemberTokenUtil;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.hibernate.validator.constraints.pl.REGON;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 公司信息表 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2021-03-23
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {

    @Resource
    private MemberService memberService;
    @Resource
    private CompanyPositionService companyPositionService;

    @Override
    public Long add(CompanyParam param){
        Company entity = getEntity(param);
        this.save(entity);
        return entity.getId();
    }

    @Override
    public void delete(CompanyParam param){
        this.removeById(getKey(param));
    }

    @Transactional
    @Override
    public void update(CompanyParam param){
        //更新会员信息
        int memberId = MemberTokenUtil.getMemberId();
        MemberParam memberParam=new MemberParam();
        //判断ID是否存在，如果不存在，则新建
        if (param.getId()!=null){
            memberParam.setCompanyId(param.getId());
            Company oldEntity = getOldEntity(param);
            Company newEntity = getEntity(param);
            ToolUtil.copyProperties(newEntity, oldEntity);
            this.updateById(newEntity);
        }else{
           Long id=this.add(param);
            memberParam.setCompanyId(id);
        }
        memberParam.setMemberId(memberId);
        memberParam.setCompany(param.getName());
        memberParam.setPostName(param.getPostName());
        memberService.update(memberParam);
    }

    @Override
    public void updateAuth(CompanyParam param) {
        param.setAddAuthTime(new Date());
        Company oldEntity = getOldEntity(param);
        Company newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public void auth(CompanyParam param) {
        //认证审核
        param.setAuthDate(new Date());
        Company oldEntity = getOldEntity(param);
        Company newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CompanyResult findBySpec(CompanyParam param){
        return baseMapper.findBySpec(param);
    }

    @Override
    public List<CompanyResult> findListBySpec(CompanyParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(CompanyParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        List<CompanyResult> list=page.getRecords();
        for(int i=0;i<list.size();i++){
            CompanyPositionParam companyPositionParam=new CompanyPositionParam();
            companyPositionParam.setCompanyId(list.get(i).getId());
            List<CompanyPositionResult> companyPositionResults=companyPositionService.findListBySpec(companyPositionParam);
            if (companyPositionResults.size()>0){
                list.get(i).setPositonName(companyPositionResults.get(0).getPostName());
                list.get(i).setSalary(companyPositionResults.get(0).getSalary());
            }
            list.get(i).setWorkTime(list.get(i).getStartTime()+"-"+list.get(i).getEndTime());
        }
        page.setRecords(list);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findPageBySpecForSelect(CompanyParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageMapList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(CompanyParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Company getOldEntity(CompanyParam param) {
        return this.getById(getKey(param));
    }

    private Company getEntity(CompanyParam param) {
        Company entity = new Company();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
